//
//  LYMessageViewController.swift
//  WeChat
//
//  Created by mac on 2018/7/24.
//  Copyright © 2018年 RRC. All rights reserved.
//

import UIKit
import SnapKit
import SwiftyJSON

class LYMessageViewController: UIViewController {

    let cellIdentifier = "cellIdentifier"
    
    fileprivate var itemDataSource = [LYMessageModel]()
    var actionFloatView:LYMessageActionFloatView!
    fileprivate var listTabelView:UITableView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        self.title = "微信"
        self.view.backgroundColor = UIColor.viewBackgroundColor
    self.navigationItem.rightButtonAction(LYAsset.Barbuttonicon_add.image, action: {
            self.actionFloatView.hide(!self.actionFloatView.isHidden)
        })
        
        listTabelView = UITableView()
        listTabelView.delegate = self
        listTabelView.dataSource = self
        self.view.addSubview(listTabelView)
        listTabelView.snp.makeConstraints { (make) in
            make.edges.equalTo(0)
        }
        listTabelView.tableFooterView = UIView()
        listTabelView.estimatedRowHeight = 65
        listTabelView.register(LYMessageTableViewCell.self, forCellReuseIdentifier: cellIdentifier)
        
        self.actionFloatView = LYMessageActionFloatView()
        self.actionFloatView.delegate = self as? ActionFloatViewDelegate
        self.view.addSubview(self.actionFloatView)
        self.actionFloatView.snp.makeConstraints { (make) in
            make.edges.equalTo(UIEdgeInsetsMake(64, 0, 0, 0))
        }
        
        

        self.fetchData()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    fileprivate func fetchData(){
        guard let JSONData = Data.ts_dataFromJSONFile("message") else {
            return
        }
        do{
            let jsonObject = try! JSON(data: JSONData)
            
            var list = [LYMessageModel]()
            for dict in jsonObject["data"].array!{
                let model = LYMessageModel(jsonData: dict )
                list.insert(model, at: list.count)
            }
            itemDataSource.insert(contentsOf: list, at: 0)
            itemDataSource.insert(contentsOf: list, at: 0)
            itemDataSource.insert(contentsOf: list, at: 0)
            itemDataSource.insert(contentsOf: list, at: 0)
            listTabelView.reloadData()
        }
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}

extension LYMessageViewController:UITableViewDelegate{
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return listTabelView.estimatedRowHeight
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let viewController = LYChatViewController()
        viewController.messageModel = self.itemDataSource[indexPath.row]
        viewController.hidesBottomBarWhenPushed = true
        self.navigationController?.pushViewController(viewController, animated: true)
    }
    
}


extension LYMessageViewController:UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return itemDataSource.count
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
//        let cellID:String = "identi"
        let cell:LYMessageTableViewCell = tableView.dequeueReusableCell(withIdentifier: cellIdentifier) as! LYMessageTableViewCell
//        if (cell == nil) {
//            cell = UITableViewCell(style: .default, reuseIdentifier: cellID)
//        }
        cell.setCellContent(itemDataSource[indexPath.row])
        
        return cell
    }
    
    
}


